package log

import (
	"fmt"

	"gitee.com/mkwchecking/sr-core/log/ilog"
	"github.com/go-kratos/kratos/v2/log"

	_ "gitee.com/mkwchecking/sr-core/log/ilog/engine/zap"
	_ "gitee.com/mkwchecking/sr-core/log/ilog/writer/stdout"
)

var (
	instanceLog ilog.Logger
)

func init() {
	instanceLog, _ = Install(ilog.StdoutConfig())
}

func Install(cfg ilog.Config) (ilog.Logger, error) {
	writer, err := ilog.InstantiateWriter(cfg.WriteOut, &cfg)
	if err != nil {
		return nil, err
	}

	ilog, err := ilog.InstantiateHandler(cfg.Engine, &cfg, writer)
	if err != nil {
		return nil, err
	}
	instanceLog = ilog
	return ilog, nil
}

func KLog() log.Logger {
	return log.With(NewKlog(instanceLog))
}

func GetDefault() ilog.Logger {
	return instanceLog
}

func With(kvs ...interface{}) ilog.Logger {
	return instanceLog.With(kvs...)
}

func Log(level ilog.Level, kvs ...interface{}) error {
	return instanceLog.Log(level, kvs...)
}

func Trace(msg string, kvs ...interface{}) {
	instanceLog.Trace(msg, kvs...)
}

func Tracef(msg string, kvs ...interface{}) {
	Trace(fmt.Sprintf(msg, kvs...))
}

func Debugf(msg string, kvs ...interface{}) {
	Debug(fmt.Sprintf(msg, kvs...))
}

func Debug(msg string, kvs ...interface{}) {
	instanceLog.Debug(msg, kvs...)
}

func Infof(msg string, kvs ...interface{}) {
	Info(fmt.Sprintf(msg, kvs...))
}

func Info(msg string, kvs ...interface{}) {
	instanceLog.Info(msg, kvs...)
}

func Warnf(msg string, kvs ...interface{}) {
	Warn(fmt.Sprintf(msg, kvs...))
}

func Warn(msg string, kvs ...interface{}) {
	instanceLog.Warn(msg, kvs...)
}

func Errorf(msg string, kvs ...interface{}) {
	Error(fmt.Sprintf(msg, kvs...))
}

func Error(msg string, kvs ...interface{}) {
	instanceLog.Error(msg, kvs...)
}

func Fatalf(msg string, kvs ...interface{}) {
	Fatal(fmt.Sprintf(msg, kvs...))
}

func Fatal(msg string, kvs ...interface{}) {
	instanceLog.Fatal(msg, kvs...)
}
